define(function (require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');


    var warehouse = {
        _shopId: "",
        _warehouseId: "",
        init: function () {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function () {
                if ($(this).parents("a").text() == "店铺仓库管理") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.initTable();
            this.bindEvent();
        },
        /**
         * 执行查询操作.
         */
        doSearch: function () {
            $('#table').DataTable().ajax.reload();
        },
        /**
         * 初始化的列表
         */
        initTable: function () {
            var self = this;
            $("#table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%",   //表格的宽度
                "sScrollXInner": "100%",   //表格的内容宽度
                "bScrollCollapse": true,  //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true,  //是否显示分页
                "bLengthChange": true,  //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": true, //显示表格信息
                "bAutoWidth": false,  //自适应宽度
                "bServerSide": true,//开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/shop/shop/searchPrivatePiFaShop'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    var _data = {};
                    $($("#J_Search").serializeArray()).each(function () {//获取查询条件
                        _data[this.name] = this.value;
                    });
                    _data.type = _data.type.split(",")
                    _data.pageBegin = aoData[3].value / aoData[4].value + 1;
                    _data.pageSize = aoData[4].value;
                    CRM.request({
                        "type": 'POST',
                        "url": sSource,
                        "dataType": "json",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": JSON.stringify(_data),
                        contentType: "application/json",
                        "success": function (req) {
                            if (req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = req.entry.num;
                                    resultData.iTotalRecords = req.entry.num;
                                    $.each(req.entry.docs, function (key, entry) {
                                        resultData.aaData.push(entry)
                                    });
                                } else {
                                    CRM.showDialog(req.message);
                                }
                                fnCallback(resultData);
                            } else {
                                CRM.showDialog(req.message);
                            }
                        }
                    });
                }, //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [
                    {
                        "sTitle": "店铺ID",
                        "mDataProp": "id",
                        "bSortable": true
                    },
                    {
                        "sTitle": "店铺名称",
                        "mDataProp": "shopName",
                        "bSortable": true
                    },
                    {
                        "sTitle": "城市",
                        "mDataProp": "city",
                        "bSortable": true
                    },
                    {
                        "sTitle": "店铺所有者",
                        "mDataProp": "ownerName",
                        "bSortable": true
                    },
                    {
                        "sTitle": "当前管理小二",
                        "mDataProp": "xiaoerName",
                        "bSortable": true
                    },
                    {
                        "sTitle": "店铺状态",
                        "mDataProp": "statusDesc",
                        "bSortable": true
                    },
                    {
                        "sTitle": "店铺类型",
                        "mDataProp": "typeDesc",
                        "bSortable": true
                    },
                    {
                        "sTitle": "经营范围",
                        "mDataProp": "tagsDesc",
                        "bSortable": true
                    },
                    {
                        "sTitle": "操作",
                        "mDataProp": "id",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = "";
                            _returnStr += '<button type="button" class="btn btn-info" onclick="javascript:showInfo(\'' + data + '\')">仓库管理</button>';
                            return _returnStr;
                        }
                    }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "full_numbers" //分页，一共两种样式，full_numbers和two_button(默认)

            });
            $("#warehouse_table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%",   //表格的宽度
                "sScrollXInner": "100%",   //表格的内容宽度
                "bScrollCollapse": true,  //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": false,  //是否显示分页
                "bLengthChange": false,  //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": false, //显示表格信息
                "bAutoWidth": false,  //自适应宽度
                "bServerSide": true,//开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/itemcenter/crm/shop/warehouse.do'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    var _data = {};
                    if (!self._shopId) {
                        return false;
                    }
                    _data.shopId = self._shopId;
                    CRM.request({
                        "type": 'GET',
                        "url": sSource,
                        "dataType": "json",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": _data,
                        contentType: "application/x-www-form-urlencoded",
                        "success": function (req) {
                            if (req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    if (req.entry.isPreSell == 0){
                                        $('#noPreSell').attr('checked',true);
                                    }
                                    else {
                                        $('#hasPreSell').attr('checked',true);
                                        $('#noPreSell').attr('disabled',true);
                                    }
                                    resultData.iTotalDisplayRecords = req.entry.num;
                                    resultData.iTotalRecords = req.entry.num;
                                    self._warehouseId = req.entry.warehouseId;
                                    $.each(req.entry.warehouseList, function (key, entry) {
                                        resultData.aaData.push(entry)
                                    });
                                } else {
                                    CRM.showDialog(req.message);
                                }
                                fnCallback(resultData);
                            } else {
                                CRM.showDialog(req.message);
                            }
                        }
                    });
                }, //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [
                    {
                        "sTitle": "",
                        "mDataProp": "id",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = "";
                            if (self._warehouseId == data) {
                                _returnStr += '<input checked type="radio" name="warehouse" value="' + data + '" /> ';
                            } else {
                                _returnStr += '<input type="radio" name="warehouse" value="' + data + '" /> ';
                            }

                            return _returnStr;
                        }
                    },
                    {
                        "sTitle": "仓库编号",
                        "mDataProp": "warehouseNo",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = CRM.htmlEncode(data);
                            return _returnStr;
                        }
                    },
                    {
                        "sTitle": "仓库名称",
                        "mDataProp": "name",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = CRM.htmlEncode(data);
                            return _returnStr;
                        }
                    },
                    {
                        "sTitle": "所属城市",
                        "mDataProp": "area",
                        "sWidth": "100px",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = CRM.htmlEncode(data);
                            return _returnStr;
                        }
                    },
                    {
                        "sTitle": "地址",
                        "mDataProp": "address",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = CRM.htmlEncode(data);
                            return _returnStr;
                        }
                    },
                    {
                        "sTitle": "仓库电话",
                        "mDataProp": "phone",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = CRM.htmlEncode(data);
                            return _returnStr;
                        }
                    },
                    {
                        "sTitle": "仓库负责人",
                        "mDataProp": "contactName",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = CRM.htmlEncode(data);
                            return _returnStr;
                        }
                    },
                    {
                        "sTitle": "负责人电话",
                        "mDataProp": "contactMobile",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = CRM.htmlEncode(data);
                            return _returnStr;
                        }
                    }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "full_numbers" //分页，一共两种样式，full_numbers和two_button(默认)

            });
        },
        /**
         * 查看详情
         * @param shopId
         */
        showInfo: function (shopId) {
            this._shopId = shopId;
            $("#info_dialog").modal('show');
            $("#info_dialog").on('shown.bs.modal', function (e) {
                $('#warehouse_table').DataTable().ajax.reload();
            })
        },
        /**
         * 保存
         */
        save: function () {
            var self = this;
            var warehouseId = $('#warehouse_table input:radio:checked').val();
            var warehouseNo = $('#warehouse_table input:radio:checked').parent().next().html();
            var isPreSell = $('#isPreSell input:radio:checked').val();
            if (warehouseId) {
                CRM.request({
                    "type": 'POST',
                    "url": CRM.getOuterUrl("/itemcenter/crm/shop/manageStock"),
                    "dataType": "json",
                    contentType: "application/json",
                    "data": JSON.stringify({
                        shopId: self._shopId,
                        warehouseNo: warehouseNo,
                        preSell: isPreSell
                        //warehouseId: warehouseId
                    }),
                    "success": function (req) {
                        if (req.status) {
                            CRM.showDialog("保存成功！");
                        } else {
                            CRM.showDialog(req.message);
                        }
                    }
                });
            } else {
                CRM.showDialog("请选择仓库")
            }
        },
        bindEvent: function () {
            var self = this;
            $(document).on('click', '#save', function () {
                if (confirm('确认保存?')) {
                    self.save();
                }
            });
            $(document).on('click', '#J_Search', function () {
                self.doSearch();
            });
        }
    };

    window.doSearch = $.proxy(warehouse.doSearch, warehouse);
    window.showInfo = $.proxy(warehouse.showInfo, warehouse);

    warehouse.init();
});